import api from '@/api/request'

const COMMUNITY_TEMPLATE_PATH_PREFIX = '/api/region/community/template'

/**
 * 下载小区模板
 */
export const downloadTemplate = (): Promise<{ data: Blob }> => {
  return api.get(`${COMMUNITY_TEMPLATE_PATH_PREFIX}/download`, {
    responseType: 'blob'
  })
}

/**
 * 上传模板文件
 */
export const uploadTemplate = (file: File): Promise<{ data: string }> => {
  const formData = new FormData()
  formData.append('file', file)
  
  return api.upload(`${COMMUNITY_TEMPLATE_PATH_PREFIX}/upload`, formData)
}

/**
 * 验证模板数据
 */
export const validateTemplate = (fileId: string): Promise<{ data: any }> => {
  return api.post(`${COMMUNITY_TEMPLATE_PATH_PREFIX}/validate`, null, {
    params: { fileId }
  })
}

/**
 * 导入模板数据
 */
export const importTemplate = (data: {
  fileId: string
  communityId: number
  skipErrors?: boolean
  overwrite?: boolean
}): Promise<{ data: any }> => {
  return api.post(`${COMMUNITY_TEMPLATE_PATH_PREFIX}/import`, data)
}

/**
 * 查询导入进度
 */
export const getImportProgress = (taskId: string): Promise<{ data: any }> => {
  return api.get(`${COMMUNITY_TEMPLATE_PATH_PREFIX}/progress/${taskId}`)
}
